package fr.poc.rest.job;

import java.net.URI;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ServerListFilter;

//@Component
//@FeignClient("pocserviceB.mydomain.net")
//@FeignClient("pocserviceB1")
public class PocUsingEurekaRibbonRemoteServcie {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IClientConfig ribbonClientConfig;
	@Autowired
	private IRule ribbonRule;
	@Autowired
	private IPing ribbonPing;
	@Autowired
	private ServerList<Server> ribbonServerList;
	@Autowired
	private ServerListFilter<Server> ribbonServerListFilter;
	@Autowired
	private ILoadBalancer ribbonLoadBalancer;
	
	private Client client = ClientBuilder.newClient();
	
	@Autowired
	private RestTemplate restTemplate;

	@Scheduled(initialDelay = 30000, fixedDelay = 10000)
	public void callHello() {
		// String uri = targetBaseUrl + "/hello";

		String vipAddress = "pocserviceB.mydomain.net";

		// using Ribbon API

		// ServiceInstance instance =
		// ribbonLoadBalancer.choose("pocserviceB.mydomain.net");
		Server instance = ribbonLoadBalancer
				.chooseServer("pocserviceB.mydomain.net");
		URI uri = URI.create(String.format("http://%s:%s/rest/hello",
				instance.getHost(), instance.getPort()));

		String greetings = null;
		//
		// if (!services.isEmpty()) {
		// ServiceInstance service = services.get(0);
		//
		// uri = "http://" + service.getHost() + ":" + service.getPort()
		// + "/rest/hello";
		//
		logger.info(">>callHello>>>>vipAdress={} , uri={}", vipAddress, uri);

		WebTarget target = client.target(uri);
		greetings = target.request(MediaType.APPLICATION_JSON_TYPE).get(
				String.class);

		logger.info(">>callHello>>>> URI:{} / REPONSE:{}", uri, greetings);

	}
	
	
	
	@Scheduled(initialDelay = 30000, fixedDelay = 10000)
	public void callHello2() {
		// String uri = targetBaseUrl + "/hello";

		String vipAddress = "pocserviceB.mydomain.net";
		String uri = "http://pocserviceB.mydomain.net/rest/hello";

		String greetings = null;

		logger.info(">>callHello>>>>vipAdress={} , uri={}", vipAddress, uri);

		greetings = restTemplate.getForObject("", String.class);

		logger.info(">>callHello2>>>> URI:{} / REPONSE:{}", uri, greetings);

	}
	

}
